package com.ruoyi.web.controller.kyhs;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.kysh.entity.TaskPlan;
import com.ruoyi.system.kysh.entity.dto.TaskPlanAddDateDto;
import com.ruoyi.system.kysh.entity.dto.TaskPlanDateDto;
import com.ruoyi.system.kysh.service.TaskPlanService;
import com.ruoyi.system.kysh.utils.MyTimeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@Api("任务作业工单")
@RestController
@RequestMapping("/kyhs/task-plan")
public class TaskPlanController {

    @Autowired
    private TaskPlanService taskPlanService;

    @ApiOperation("以工单编号和工单名称为条件分页查询（工单编号和工单名选填）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ticketNum",value = "工单编号",dataTypeClass = String.class),
            @ApiImplicitParam(name = "ticketName",value = "工单名",dataTypeClass = String.class),
            @ApiImplicitParam(name = "page",value = "当前页数"),
            @ApiImplicitParam(name = "pageSize",value = "每页容量")
    })

    @GetMapping("/select-in-page")
    public AjaxResult selectByTaskIdAndTaskName(String ticketNum,String ticketName,int page,int pageSize){
        TaskPlanDateDto taskPlanDateDto = taskPlanService.selectByTaskIdAndTaskName(ticketNum, ticketName, page, pageSize);
        return AjaxResult.success("操作成功",taskPlanDateDto);
    }

    @ApiOperation("根据工单编号查询工单详细信息")
    @ApiImplicitParam(name = "ticketNum",value = "工单编号",dataTypeClass = String.class)
    @GetMapping("/select-detail-by-num")
    public AjaxResult selectDetailByTicketNum(String ticketNum){
        TaskPlan taskPlan=taskPlanService.selectDetailByTicketNum(ticketNum);
        if(taskPlan==null){
            return AjaxResult.error("工单编号不存在");
        }
        return AjaxResult.success("操作成功",taskPlan);
    }

    @ApiOperation("根据工单编号删除工单")
    @ApiImplicitParam(name = "ticketNum",value = "工单编号",dataTypeClass = String.class)
    @DeleteMapping("/delete-by-num")
    public AjaxResult deleteByTicketNum(@RequestParam String ticketNum){
        boolean b=taskPlanService.deleteByTicketNum(ticketNum);
        if(!b){
            return AjaxResult.error("删除失败");
        }
        return AjaxResult.success("删除成功");
    }

    @ApiOperation("新建工单信息 必填内容见原型")
    @PostMapping("/save")
    public AjaxResult save(@RequestBody TaskPlan taskPlan){
        boolean b=taskPlanService.saveTaskPlan(taskPlan);
        if(!b){
            return AjaxResult.warn("信息相关内容已存在");
        }
        return AjaxResult.success("新建成功");
    }

    @ApiOperation("编辑工单信息 必填内容见原型")
    @PutMapping("/update")
    public AjaxResult updateTaskPlan(@RequestBody TaskPlan taskPlan){
        boolean b=taskPlanService.updateTaskPlan(taskPlan);
        if(!b){
            return AjaxResult.error("编辑失败，请核对信息后重试");
        }
        return AjaxResult.success("编辑成功");
    }

    @ApiOperation("根据任务工单主键id发布")
    @ApiImplicitParam(name = "id",value = "工单主键",dataTypeClass = Long.class)
    @PutMapping("/issued")
    public AjaxResult issuedTask(@RequestParam Long id){
        TaskPlan taskPlan=taskPlanService.selectByTaskId(id);
        if(taskPlan==null){
            return AjaxResult.warn("信息不存在");
        }
        if(taskPlan.getStatus().equals("0")){
            taskPlanService.updateTaskStatus(id,"1");
            return AjaxResult.success("下达成功");
        }
        return AjaxResult.warn("任务已下达，请勿重复操作");
    }

    @PostMapping("/issued-with-save")
    public AjaxResult issuedTaskWithSave(@RequestBody TaskPlan taskPlan){
        if (taskPlan==null||"".equals(taskPlan.toString())) {
            return AjaxResult.warn("请将信息填写完整");
        }
        boolean b=taskPlanService.saveTaskPlanWithIssued(taskPlan);
        if (b) {
            return AjaxResult.success("下达成功");
        }
        return AjaxResult.warn("任务已下达，请勿重复操作");

    }

    @ApiOperation("根据任务工单主键id撤回")
    @ApiImplicitParam(name = "id",value = "工单主键",dataTypeClass = Long.class)
    @PutMapping("/withdraw")
    public AjaxResult withdrawTask(@RequestParam Long id){
        TaskPlan taskPlan=taskPlanService.selectByTaskId(id);
        if(taskPlan==null){
            return AjaxResult.warn("信息不存在");
        }
        if(taskPlan.getStatus().equals("1")){
            taskPlanService.updateTaskStatus(id,"0");
            return AjaxResult.success("撤回成功");
        }
        return AjaxResult.warn("任务未下达或已开始执行，无法撤回");
    }

}
